package assetservicelogic

import (
	"context"
	"github.com/zeromicro/go-zero/core/logx"
	"property_service/dao/model"
	"property_service/dao/query"
	"property_service/internal/svc"
	"property_service/property_service"
	"property_service/utils/BeanUtils"
)

type AssetDepreciationPageLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewAssetDepreciationPageLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AssetDepreciationPageLogic {
	return &AssetDepreciationPageLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// AssetDepreciationPage 分页查询折旧管理表项
func (l *AssetDepreciationPageLogic) AssetDepreciationPage(in *property_service.AssetDepreciationPageReq) (*property_service.AssetDepreciationPageRes, error) {
	// 定义一个折旧管理表模型对象切片用于存储查询结果
	var data []*model.AssetDepreciation
	// 获取折旧管理表模型的查询构造器
	m := query.Use(l.svcCtx.Gorm).AssetDepreciation
	// 构建查询条件：根据租户ID查询
	do := m.WithContext(l.ctx).Where(m.TenantID.Eq(in.Where.TenantID))
	// 动态构建查询条件
	if in.Where.ID > 0 {
		do = do.Where(m.ID.Eq(in.Where.ID)) // ID
	} else {
		// 处理字符类型字段，进行模糊查询
		if in.Where.AssetCode != "" {
			do = do.Where(m.AssetCode.Like("%" + in.Where.AssetCode + "%")) // 资产编码
		}
		if in.Where.CardNumber != "" {
			do = do.Where(m.CardNumber.Like("%" + in.Where.CardNumber + "%")) // 卡片编号
		}
		if in.Where.AssetName != "" {
			do = do.Where(m.AssetName.Like("%" + in.Where.AssetName + "%")) // 资产名称
		}
		if in.Where.AssetCategory != "" {
			do = do.Where(m.AssetCategory.Like("%" + in.Where.AssetCategory + "%")) // 资产分类
		}
		if in.Where.DepreciationPeriod != "" {
			do = do.Where(m.DepreciationPeriod.Like("%" + in.Where.DepreciationPeriod + "%")) // 折旧期间
		}
		if in.Where.DataIdentifier != "" {
			do = do.Where(m.DataIdentifier.Like("%" + in.Where.DataIdentifier + "%")) // 数据标识
		}
		if in.Where.Warehouse != "" {
			do = do.Where(m.Warehouse.Like("%" + in.Where.Warehouse + "%")) // 库房
		}
		if in.Where.UsingDept != "" {
			do = do.Where(m.UsingDept.Like("%" + in.Where.UsingDept + "%")) // 使用科室
		}
		if in.Where.DepreciationMethod != "" {
			do = do.Where(m.DepreciationMethod.Like("%" + in.Where.DepreciationMethod + "%")) // 折旧方法
		}
		if in.Where.StatusName != "" {
			do = do.Where(m.StatusName.Like("%" + in.Where.StatusName + "%")) // 状态名称
		}
		if in.Where.Remarks != "" {
			do = do.Where(m.Remarks.Like("%" + in.Where.Remarks + "%")) // 备注
		}
		if in.Where.DepreciationStatus != "" {
			do = do.Where(m.DepreciationStatus.Like("%" + in.Where.DepreciationStatus + "%")) // 折旧状态
		}

		//如果是根据资产id数组查询
		if len(in.Where.AssetIDs) > 0 {
			do = do.Where(m.AssetID.In(in.Where.AssetIDs...)) // 资产ID
		}
		// 处理时间类型字段，进行精确查询
		if in.Where.PurchaseDate != "" {
			do = do.Where(m.PurchaseDate.Eq(BeanUtils.GetDateFormat(in.Where.PurchaseDate))) // 购置日期
		}

		// 处理数字类型字段，进行精确查询
		if in.Where.AssetID > 0 {
			do = do.Where(m.AssetID.Eq(in.Where.AssetID)) // 资产ID
		}
		if in.Where.ExpectedUsefulLife > 0 {
			do = do.Where(m.ExpectedUsefulLife.Eq(in.Where.ExpectedUsefulLife)) // 预计使用年限（月）
		}
		if in.Where.DepreciatedMonths > 0 {
			do = do.Where(m.DepreciatedMonths.Eq(in.Where.DepreciatedMonths)) // 已折旧月份
		}
		if in.Where.AssetCategoryID > 0 {
			do = do.Where(m.AssetCategoryID.Eq(in.Where.AssetCategoryID)) // 资产分类ID
		}
		if in.Where.UsingDeptID > 0 {
			do = do.Where(m.UsingDeptID.Eq(in.Where.UsingDeptID)) // 使用科室ID
		}
		if in.Where.DepreciationMethodID > 0 {
			do = do.Where(m.DepreciationMethodID.Eq(in.Where.DepreciationMethodID)) // 折旧方法ID
		}
		if in.Where.StatusID > 0 {
			do = do.Where(m.StatusID.Eq(in.Where.StatusID)) // 状态ID
		}
		if in.Where.StockID > 0 {
			do = do.Where(m.StockID.Eq(in.Where.StockID)) // 仓库ID
		}
	}

	// 定义一个变量用于存储总记录数
	var count int64

	// 判断是否需要分页
	if in.Page+in.Limit <= 1 {
		// 如果不需要分页，查询所有记录
		data, _ = do.Find()
		// 设置总记录数为查询结果的长度
		count = int64(len(data))
	} else {
		// 如果需要分页，查询指定页的数据和总记录数
		data, count, _ = do.FindByPage(int((in.Page-1)*in.Limit), int(in.Limit))
	}

	// 将查询结果转换为响应对象
	list, _ := BeanUtils.CopyProperties(data, property_service.AssetDepreciation{})

	// 返回查询结果，包括总记录数和数据列表
	return &property_service.AssetDepreciationPageRes{
		Count: count,
		List:  list.([]*property_service.AssetDepreciation),
	}, nil
}
